package com.threeigtache.threeigproject;
import android.app.Activity;
import android.content.DialogInterface;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.ViewDragHelper;
import android.util.DisplayMetrics;
import android.view.View;

import android.widget.LinearLayout;
import android.widget.TextView;

import com.flyco.tablayout.SlidingTabLayout;
import com.threeigtache.threeigproject.mianwork.WalletActivity;
import com.threeigtache.weigit.NoScrollViewPager;

import java.lang.reflect.Field;

public class MainActivity  extends BasicActivity implements View.OnClickListener {

    private DrawerLayout mDrawer;
    private LinearLayout mMoney;
    private LinearLayout mCustomer;
    private LinearLayout mSetting;
    private LinearLayout mFix;
    private SlidingTabLayout mTabLayout;
    private NoScrollViewPager mViewPager;
    private TextView mShare;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_main);
        showBody();
         findViewById(R.id.user).setOnClickListener(this);
         findViewById(R.id.message).setOnClickListener(this);

        mTabLayout = (SlidingTabLayout)findViewById(R.id.slidingtablayout);
        mViewPager = (NoScrollViewPager)findViewById(R.id.viewpager);
        mShare = (TextView)findViewById(R.id.share);
        mDrawer = (DrawerLayout)findViewById(R.id.drawer);

        //侧滑菜单
        mMoney = (LinearLayout) findViewById(R.id.money);
        mCustomer = (LinearLayout)findViewById(R.id.customer);
        mSetting = (LinearLayout)findViewById(R.id.setting);
        mFix = (LinearLayout)findViewById(R.id.fix);

        mMoney.setOnClickListener(this);
        mCustomer.setOnClickListener(this);
        mSetting.setOnClickListener(this);
        mFix.setOnClickListener(this);
        mShare.setOnClickListener(this);

        //TODO:解决全屏滑动而不能出现侧滑
//        setDrawerLeftEdgeSize(MainActivity.this, mDrawer, 1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //TODO:先判断是否登录了，并且注册完成了
            case R.id.money:
                skipOtherActivity(WalletActivity.class, false);
                break;
            //拨打电话
            case R.id.customer:
//                hasPermission();
                android.app.AlertDialog.Builder builder2 = new android.app.AlertDialog.Builder(MainActivity.this);
                builder2.setMessage("拨打客服电话4000209777");
                builder2.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        //需要后台提供
//                        tellPhone("4000209777");
                        dialog.dismiss();
                    }
                });
                android.app.AlertDialog dialog = builder2.create();
                dialog.show();
                break;
            case R.id.setting:

//                skipOtherActivity(SettingActivity.class,false);
                break;
            case R.id.fix:
//                skipOtherActivity(EquipFixActivity.class,false);
                break;
            case R.id.user:
                //TODO:打开侧滑菜单
                mDrawer.openDrawer(GravityCompat.START);
                break;
            case R.id.message:
                break;
            case R.id.share:
                break;
        }
    }









    //DrawerLayout不能全屏滑动的问题
    public void setDrawerLeftEdgeSize(Activity activity, DrawerLayout drawerLayout, float displayWidthPercentage)
    {
            if (activity == null || drawerLayout == null)
                return;
            try
            {
                // find ViewDragHelper and set it accessible
                Field leftDraggerField = drawerLayout.getClass().getDeclaredField("mLeftDragger");
                leftDraggerField.setAccessible(true);
                ViewDragHelper leftDragger = (ViewDragHelper) leftDraggerField.get(drawerLayout);
                Field edgeSizeField = leftDragger.getClass().getDeclaredField("mEdgeSize");
                edgeSizeField.setAccessible(true);
                int edgeSize = edgeSizeField.getInt(leftDragger);
                DisplayMetrics dm = new DisplayMetrics();
                activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
                edgeSizeField.setInt(leftDragger, (int) (dm.widthPixels * displayWidthPercentage));
            } catch (Exception e)
            {
            }
    }


}
